package de.szut.traineeslife;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;

import de.szut.traineeslife.utils.PreferenceHandler;

public class FirstStartActivity extends SherlockActivity {

	private ImageView logo;
	private TextView sloganTextView;
	private String sloganText = "Mach dir das Leben leichter...";
	private Handler handler;
	private PreferenceHandler prefsHandler;

	/* Creates the first start page, an starts a Thread for Slogaen
	 * 
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		prefsHandler = new PreferenceHandler(getBaseContext());
		// if
		// (prefsHandler.getPreferences().contains(prefsHandler.getPREFS_KEY()))
		// {
		if (getSharedPreferences(prefsHandler.getPREFS_KEY(), MODE_PRIVATE) != null) {
			setContentView(R.layout.first_start_layout);
			sloganTextView = (TextView) findViewById(R.id.sloganTextView);
			logo = (ImageView) findViewById(R.id.start_logo);

			sloganTextView.setText("");
			handler = new Handler();
		} else {
			Intent i = new Intent(FirstStartActivity.this, TraineesLifeActivity.class);
			i.putExtra("PREFS", prefsHandler.getPreferences().getString(prefsHandler.getPREFS_KEY(), ""));
			startActivity(i);
			FirstStartActivity.this.finish();

		}
	}

	@Override
	public void onResume() {
		super.onResume();

		sloganTextView.setText("");

		ShowSlogan task = new ShowSlogan(this);
		task.execute();
	}

	private class ShowSlogan extends AsyncTask<String, Void, String> {
		private Context mContext;

		public ShowSlogan(Context context) {
			mContext = context;

		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... urls) {

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			for (int i = 0; i < sloganText.length() + 1; i++) {

				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				showSlogan(i);
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return "";
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Intent i = new Intent(FirstStartActivity.this, AuthenticationActivity.class);
			startActivity(i);
			FirstStartActivity.this.finish();

		}

	}

	// @Override
	// protected void onDestroy() {
	// super.onDestroy();
	// Log.d( "DESTROY", "DESTROY" );
	//
	// }

	public void showSlogan(final int counter) {

		handler.post(new Runnable() {

			public void run() {
				sloganTextView.setText(sloganText.substring(0, counter));
			}
		});

	}

}
